//
//  MineViewController.m
//  TestApplication
//
//  Created by li cheng on 16/8/26.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "MineViewController.h"
#import "BaseCollectionView.h"
#import "Masonry.h"
// 和之前的一样的, 就拿过来用了
#import "CVCellOfChioceButton.h"
#import "BaseTableView.h"
#import "BaseView.h"
#import "MJMacro.h"
#import "TVCellOfMine.h"
#import "VCOfLogining.h"
#import "UIImageView+WebCache.h"
#import "VCOfSetting.h"
#import "VCOfUserProfile.h"
// 我的收藏 VC
#import "VCOfCollec.h"
// 我的发布 VC
#import "VCOfMyPublish.h"
#import "BaseLabel.h"
#import "BaseButton.h"
//
#import "VCOfUnrealizedViewController.h"
#import "VCOfFirstLoginWay.h"
@interface MineViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet BaseButton *backGroundButton;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) BaseCollectionView *mineCollectionView;
@property (weak, nonatomic) IBOutlet UIImageView *AvatarImageView;
@property (weak, nonatomic) IBOutlet BaseLabel *likeNumebrAndPeopleLabel;
@property (nonatomic, strong) NSArray *arrayOfCollecttionViewIcon;
@property (nonatomic, strong) NSArray *arrayOfCollectionLabelText;
@property (nonatomic, strong) NSArray *arrayOfTableViewIcon;
@property (nonatomic, strong) NSArray *arrayOfTableViewLabelText;
@property (weak, nonatomic) IBOutlet BaseLabel *pleaseLoginOrNicename;
@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 小图标的数组
    _arrayOfCollecttionViewIcon = @[@"Bell_64", @"Consulting_64", @"Publish_64", @"Collect_64"];
    _arrayOfCollectionLabelText = @[@"消息中心", @"咨询管理", @"我的发布", @"我的收藏"];
    _arrayOfTableViewIcon = @[@"CurriculumManagement_32", @"Concern_32", @"Feedback_32", @"Setting_32"];
    _arrayOfTableViewLabelText = @[@"课程管理", @"关注/粉丝", @"意见反馈", @"设置"];
    // 创建 collectionView
    [self createCollectView];
    // 创建 tableView
    [self createTableView];
    
}
#pragma mark - 界面将要出现的实现
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSUserDefaults *user =[NSUserDefaults standardUserDefaults];
    NSString *avator = [user objectForKey:@"avatar"];
    NSString *nickname = [user objectForKey:@"nickname"];
    
    if (avator) {
        [self.AvatarImageView sd_setImageWithURL:[NSURL URLWithString:avator] placeholderImage:[UIImage imageNamed:@"0"]];
    } else {
        self.AvatarImageView.image = [UIImage imageNamed:@"0"];
    }
    if (nickname) {
        self.pleaseLoginOrNicename.text = nickname;
    } else {
        self.pleaseLoginOrNicename.text = @"请登录";
    }
//    NSString *user_id = [user objectForKey:@"user_id"];
    NSLog(@"发水电费的送货单上发的啥好的时候多少发的啥好的时候多少发的啥都是 回答颠三倒四%@%@", user_id, nickname);
}
#pragma mark - 点击后面蓝色背景实现的方法
- (IBAction)clickBackButton:(id)sender {
    NSUserDefaults *user =[NSUserDefaults standardUserDefaults];
    NSString *avator = [user objectForKey:@"avatar"];
    if (avator) {
        NSLog(@"已经登录了");
        VCOfUserProfile *vcOfUserProfile = [[VCOfUserProfile alloc] initWithNibName:@"VCOfUserProfile" bundle:[NSBundle mainBundle]];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vcOfUserProfile];
        [self presentViewController:navi animated:YES completion:^{
            
        }];
    } else {
        VCOfFirstLoginWay *vcOfFirstLoginWay = [[VCOfFirstLoginWay alloc] initWithNibName:@"VCOfFirstLoginWay" bundle:[NSBundle mainBundle]];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vcOfFirstLoginWay];
        [self presentViewController:navi animated:YES completion:^{
            
        }];
    }
}
#pragma mark - 创建子控件
- (void)createCollectView{
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    self.mineCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
    self.mineCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_mineCollectionView];
    [self.mineCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_backGroundButton.mas_bottom).offset(0);
        make.left.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(_backGroundButton.mas_height).multipliedBy(1 / 3.0f);
    }];
    self.mineCollectionView.delegate = self;
    self.mineCollectionView.dataSource = self;
    [self.mineCollectionView registerNib:[UINib nibWithNibName:@"CVCellOfChioceButton" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CVCellOfChioceButton"];
}
#pragma mark - 创建 tableView
- (void)createTableView{
    // 创建背景灰色的 View
    BaseView *backgroundView = [[BaseView alloc] init];
    backgroundView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
    [self.view addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_mineCollectionView.mas_bottom).offset(0);
        make.left.right.bottom.equalTo(self.view).offset(0);
    }];
    // 创建 TableView
    BaseTableView *mineTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT * 0.35) style:UITableViewStylePlain];
    mineTableView.bounces = NO;
    mineTableView.backgroundColor = [UIColor greenColor];
    [backgroundView addSubview:mineTableView];
    // 指定代理人
    mineTableView.delegate = self;
    mineTableView.dataSource = self;
    // 注册 cell
    [mineTableView registerClass:[TVCellOfMine class] forCellReuseIdentifier:@"TVCellOfMine"];
    [mineTableView registerNib:[UINib nibWithNibName:@"TVCellOfMine" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfMine"];
}
#pragma mark - 视图完成布局
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.layout.itemSize = CGSizeMake(_backGroundButton.bounds.size.height / 3, _backGroundButton.bounds.size.height / 3);
    CGFloat margin = (_backGroundButton.bounds.size.width - _backGroundButton.bounds.size.height / 3 * 4) / 5;
    self.layout.minimumLineSpacing = 0;
    if (IS_IPHONE_5) {
        self.layout.minimumLineSpacing = 0;
    } else {
        self.layout.minimumInteritemSpacing = margin;
    }
    self.layout.sectionInset = UIEdgeInsetsMake(0, margin, 0, margin);
    // 修改头像的圆角
    self.AvatarImageView.layer.cornerRadius = self.AvatarImageView.bounds.size.width / 2;
    self.AvatarImageView.clipsToBounds = YES;
}
#pragma mark - tableView 代理方法 row 数量的确定
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}
#pragma mark - tableView 代理方法 row 高度的确定
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_WIDTH, SCREEN_HEIGHT * 0.35 / 4;
}
#pragma mark - tableView 代理方法 cell 的赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TVCellOfMine *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfMine" forIndexPath:indexPath];
    cell.myImageView.image = [UIImage imageNamed:_arrayOfTableViewIcon[indexPath.row]];
    cell.myLabel.text = _arrayOfTableViewLabelText[indexPath.row];
    return cell;
}
#pragma mark - collectionView 代理方法 item 数量的确定
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4;
}
#pragma mark - collectionView 代理方法 cell 的赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CVCellOfChioceButton *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVCellOfChioceButton" forIndexPath:indexPath];
    [cell.myButton setImage:[UIImage imageNamed:_arrayOfCollecttionViewIcon[indexPath.row]] forState:UIControlStateNormal];
    cell.myLabel.font = [UIFont systemFontOfSize:10];
    cell.myLabel.text = _arrayOfCollectionLabelText[indexPath.row];
    return cell;
}
#pragma mark - collectionView 的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld", indexPath.row);
    if (indexPath.row == 0 || indexPath.row == 1) {
        VCOfUnrealizedViewController *vcOfUnrealizedViewController = [[VCOfUnrealizedViewController alloc] initWithNibName:@"VCOfUnrealizedViewController" bundle:[NSBundle mainBundle]];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vcOfUnrealizedViewController];
        [self presentViewController:navi animated:YES completion:^{
            
        }];
        
    }
    if (indexPath.row == 2) {
        VCOfMyPublish *vcOfMyPublish = [[VCOfMyPublish alloc] init];
        UINavigationController *naiv = [[UINavigationController alloc] initWithRootViewController:vcOfMyPublish];
        [self presentViewController:naiv animated:YES completion:^{
            
        }];
    }
    if (indexPath.row == 3) {
        VCOfCollec *vcOfCollec = [[VCOfCollec alloc] init];
        UINavigationController *naiv = [[UINavigationController alloc] initWithRootViewController:vcOfCollec];
        [self presentViewController:naiv animated:YES completion:^{
            
        }];
    }
}
#pragma mark - tableView cell 的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3) {
        NSLog(@"设置");
        VCOfSetting *vcOfSetting = [[VCOfSetting alloc] init];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vcOfSetting];
        [self presentViewController:navi animated:YES completion:^{
            
        }];
    } else {
        [self pleaseWait];
    }
    
}
#pragma mark - 还未开放此功能
- (void)pleaseWait{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"这个功能目前还没有开放" message:@"你可以尝试别的东西" preferredStyle:UIAlertControllerStyleActionSheet];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:alertController animated:YES completion:^{
        
    }];
}
#pragma mark - 其他, 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
